package dtd.PHS.VND_ExchangeRate.ui.adapters;

import java.util.Arrays;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import dtd.PHS.VND_ExchangeRate.R;
import dtd.PHS.VND_ExchangeRate.data.ListRates;
import dtd.PHS.VND_ExchangeRate.data.RateItem;
import dtd.PHS.VND_ExchangeRate.utils.Helpers;

public class ChooseCurrencyAdapter extends BaseAdapter {


	private ListRates listRates;
	protected boolean[] checked;

	public ChooseCurrencyAdapter(ListRates listRates) {
		super();		
		this.listRates = listRates;
		initCheck();
	}

	private void initCheck() {
		checked = new boolean[listRates.size()];
		String selectedCurrencies = Helpers.getPreference(Helpers.PREF_SELECTED_CURRENCIES);
		if ( selectedCurrencies == null ) {
			Arrays.fill(checked, false);
		} else {
			String[] words = selectedCurrencies.split(",");
			Arrays.fill(checked, false);
			for(int i = 0 ; i < listRates.size() ; i++) {
				RateItem item = listRates.get(i);
				for(int j = 0 ; j < words.length ; j++)
					if ( words[j].trim().toLowerCase().equals(item.getCurrencyName().toLowerCase())) {
						checked[i] = true;
						break;
					}
			}
		}
	}

	@Override
	public int getCount() {
		return listRates.size();
	}

	@Override
	public Object getItem(int position) {
		return listRates.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private class Holder {
		ImageView ivFlag;
		CheckBox cb;
		TextView tvFullCurrencyName;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v  = convertView;
		Holder holder = null;
		if ( v == null ) {
			v = Helpers.inflateView(R.layout.send_sms_item, null);
			holder = new Holder();
			holder.ivFlag = (ImageView) v.findViewById(R.id.ivFlag);
			holder.tvFullCurrencyName = (TextView) v.findViewById(R.id.tvFullName);
			holder.cb = (CheckBox) v.findViewById(R.id.cb);			
			v.setTag(holder);
		} else holder = (Holder) v.getTag();
		
		updateHolder(position, holder);
		
		return v;
		
	}

	private void updateHolder(final int position, Holder holder) {
		RateItem item = listRates.get(position);
		holder.ivFlag.setImageResource(Helpers.getFlagIconName(item.getCurrencyName()));
		
		holder.tvFullCurrencyName.setText(item.getFullName());
		
		holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				checked[position] = isChecked;
			}
		});
		holder.cb.setChecked(checked[position]);
	}

	public ListRates getSelectedRates() {
		ListRates rates = new ListRates();
		for(int i= 0 ; i < listRates.size() ; i++) {
			if ( checked[i] ) 
				rates.add(listRates.get(i));
		}
		return rates;
	}



}
